package org.hhchat.leetcode.palindrome;

/**
 * Created by hh23485 on 2017/8/22.
 * Determine whether an integer is a palindrome. Do this without extra space;
 */

//不可以转换成字符串，不可以逆序整数（会溢出）
//做除法的时候除到一半，比较一下是否相等

public class code9 {
    static class Solution {
        public boolean isPalindrome(int x) {
            if (x<0 || (x!=0 && x%10==0)) return false;
            int rev = 0;
            // x=456 rev=45 -> stop
            while (x>rev){
                rev = rev*10 + x%10;
                x = x/10;
            }
            return (x==rev || x==rev/10);
        }
    }

    public static void main(String[] args){
        Solution solution = new Solution();
        System.out.println(solution.isPalindrome(45654));
    }
}
